<?php

/**
 * viewimages actions.
 *
 * @package    comande
 * @subpackage viewimages
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class viewimagesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
   public function preExecute()
  {
     if(sfConfig::get('app_domain')=='server')
     {
          $simpleSAMLAuth = new SimpleSAML_Auth_Simple('default-sp');
          if (!$simpleSAMLAuth->isAuthenticated())
          {
               return $this->redirect('saml/logout');
          }
     }    

  }
  
  public function executeIndex(sfWebRequest $request)
  {
    
    $id = $request->getParameter('id');
    $username = "grafilab"; // username
    $key = "5428d56bf36120fc2b5414393df70046"; // api key
    // Connect to Rackspace
    $auth = new CF_Authentication($username, $key);

    // Returns a valid storage token and allows you
    // to connect to the Cloud Files Platform
    $auth->authenticate();

    // Allows us to connect to Cloud Files and
    // make changes to containers
    $conn = new CF_Connection($auth);

    // Get the container where the client's assets are stored
    $cont = $conn->get_container('ComandeFiles');

    // Return Array of the objects inside the client's container
   /* $objs = $cont->list_objects();

    // Get the URI for the client's container
    $cdnuri = $cont->cdn_uri . "/";
    */

    // Go through the Array of objects and output them with links

    /*
     * obtengo los nombres de los
     */
    $array_nombres_images = array();
    $array_images = array();
    
    $fecha = date("Y-m-d");
    $new_criteria = new Criteria();
    $new_criteria ->add(DetalleConsumoUsuarioPeer::USUARIO_ID,$id);
    $new_criteria ->add(DetalleConsumoUsuarioPeer::FECHA, $fecha);

    $obj_encontrado = DetalleConsumoUsuarioPeer::doSelectOne($new_criteria);

    if(is_object($obj_encontrado)){
        
        $nombres_imagenes = $obj_encontrado->getArrayIdProgramas();

        if($nombres_imagenes != null){
            $obj = json_decode($nombres_imagenes);

             foreach ($obj->{'images'} as $row){
                              foreach($row as $file){
                                $array_nombres_images[] =  $file;
                              }

                        }


        }else{
            $array_nombres_images = null;
        }
    }else{
        $array_nombres_images = null;
    }
 
    if(count($array_nombres_images)>0){
         for ($i = 0; $i < (count($array_nombres_images)); $i++) {
            try{
                    $doc = $cont->get_object($array_nombres_images[$i]);
                    $data = $doc->read();
                    $array_images[] = base64_encode($data);
            }catch (Exception $e){
              echo 'no existe';
            }
    }
 }
   $this->lista_files_images = $array_images;

    /*
      $parameter = $request->getParameter('image');
      $username = "grafilab";
      $key = "5428d56bf36120fc2b5414393df70046";
      $auth = new CF_Authentication($username, $key);
      $auth->authenticate();
      $conn = new CF_Connection($auth);
      $my_docs  = $conn->get_container('ComandeFiles');
//    $cont  = $conn->get_container('ComandeFiles');

	 try{
      		$doc = $my_docs->get_object($parameter);
	  }catch (Exception $e){
               echo 'no existe';
        }

      //   $doc->save_to_filename('/var/www/images/copia-prueba.jpg');


         $data = $doc->read();
         $this->file= base64_encode($data);

     */

  }

  

}
